"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomDao = void 0;
const DbConfig_1 = require("../../src/config/DbConfig");
const GameConfig_1 = require("../../src/config/GameConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
const RoomEntity_1 = require("../entity/RoomEntity");
class RoomDao extends DaoBase_1.DaoBase {
    static getRepository() {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepositoryBase(RoomEntity_1.RoomEntity, this.getGameDbName());
            return repository;
        });
    }
    static is_room_exist(roomid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select id from " + this.DB_NAME + "." + this.Tb_Name + " where `id`=?;";
            let sqlParams = [roomid];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_club_room_count(clubid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = `select count(room_id) as count from t_rooms where clubid = ?`;
            let sqlParams = [clubid];
            let results = yield client.queryPromise(sql, sqlParams);
            // Logger.log("get_club_room_count=sql=", clubid, results)
            if (results && results.length > 0) {
                return results[0]["count"];
            }
            else {
                return 0;
            }
        });
    }
    static get_all_room() {
        return __awaiter(this, void 0, void 0, function* () {
            let sql = "select * from  t_rooms;";
            let repository = yield this.getRepository();
            let results = yield repository.query(sql);
            if (results && results.length) {
                return results;
            }
            return [];
        });
    }
    static insert_room(model) {
        return __awaiter(this, void 0, void 0, function* () {
            let repository = yield this.getRepository();
            let entity = new RoomEntity_1.RoomEntity();
            entity.room_id = model.roomId;
            entity.name = model.name;
            entity.roomType = model.roomType;
            entity.game_type = model.gameType;
            entity.creator = model.creator;
            entity.clubid = model.clubid;
            entity.num_of_turns = model.numOfTurns;
            entity.create_time = DateUtil_1.default.formatDateStr(model.createTime);
            entity.server_id = model.serverId;
            entity.conf_base = JSON.stringify(model.roomConfigBase);
            if (model.gameType == GameConfig_1.GameConfig.Game_Type.Mj) {
                entity.config_game = JSON.stringify(model.roomConfigMj);
                entity.game_info_detail = JSON.stringify(model.gameInfoMj);
            }
            entity.playerList = JSON.stringify(model.playerList);
            entity.game_info_base = JSON.stringify(model.gameInfoBase);
            entity = yield repository.save(entity);
            return entity;
        });
    }
    static clear_all_room() {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "delete from t_rooms ";
            let rows = yield client.queryPromise(sql);
            if (rows && rows["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_room(model) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "update t_rooms set `num_of_turns`=?, `playerList`=?   where `room_id`=? ";
            let sqlParams = [model.num_of_turns, JSON.stringify(model.playerList), model.room_id];
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static delete_room(roomids) {
        return __awaiter(this, void 0, void 0, function* () {
            if (roomids.length <= 0)
                return;
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "delete from t_rooms  where `room_id`=? ";
            let sqlParams = [roomids[0]];
            for (let i = 1; i < roomids.length; i++) {
                sql += " or `room_id`=?";
                sqlParams.push(roomids[i]);
            }
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("delete_room==", sql, sqlParams, rows);
            if (rows && rows["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_room(room_id) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from  t_rooms where `room_id`=" + room_id;
            let rows = yield client.queryPromise(sql);
            if (rows && rows.length > 0) {
                return rows[0];
            }
            return null;
        });
    }
    static get_ziyou_room_list(gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from  t_rooms where `room_type`=4 and `gameType`=?";
            let sqlParams = [gameType];
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows.length > 0) {
                return rows;
            }
            return [];
        });
    }
}
exports.RoomDao = RoomDao;
RoomDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
RoomDao.Tb_Name = "t_rooms";
